#include "globalvars.h"
/** 头文件 **/
#include <string.h>
#include <stdlib.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

int g_led_control_flag = 0; //灯的远程控制
int g_led_color = 0; //灯的颜色
int g_led_work_mode = 0; //灯光的四个模式
// int led_control_flag = 0; //灯光的远程控制
// int g_nfc_flag = 0; //nfc的标志
// int g_wifi_flag = 0; //wifi定位的标志
// int g_led_flag = 0;  //光照强度标志

int g_window_control_flag = 0; //窗户的远程控制标志
int g_window_work_mode = 0; //窗户的工作模式

int g_door_control_flag = 0; //门的远程控制标志
int g_door_work_mode = 0; //门的工作模式

int g_garagedoor_control_flag = 0; //车库门远程控制标志

int door_data;
int window_data;
int led_data;
int garagedoor_data;
// char g_senddata[21] = "000000000000000000000";

// int g_recdata = 0;

void data_translate(void)
{
    if(door_data == 10)
    {
        g_door_work_mode = 1;
        g_door_control_flag = 0;
    }
    if(door_data == 11)
    {
        g_door_work_mode = 1;
        g_door_control_flag = 1;
    }
    if(door_data == 12)
    {
        g_door_work_mode = 1;
        g_door_control_flag = 2;
    }
    if(door_data == 20)
    {
        g_door_work_mode = 2;
        g_door_control_flag = 0;
    }
    if(door_data == 21)
    {
        g_door_work_mode = 2;
        g_door_control_flag = 1;
    }
    if(door_data == 22)
    {
        g_door_work_mode = 2;
        g_door_control_flag = 2;
    }
    if(door_data == 30)
    {
        g_door_work_mode = 3;
        g_door_control_flag = 0;
    }
    if(door_data == 31)
    {
        g_door_work_mode = 3;
        g_door_control_flag = 1;
    }
    if(door_data == 32)
    {
        g_door_work_mode = 3;
        g_door_control_flag = 2;
    }
    if(door_data == 40)
    {
        g_door_work_mode = 4;
        g_door_control_flag = 0;
    }
    if(door_data == 41)
    {
        g_door_work_mode = 4;
        g_door_control_flag = 1;
    }
    if(door_data == 42)
    {
        g_door_work_mode = 4;
        g_door_control_flag = 2;
    }

    if(window_data == 10)
    {
        g_window_work_mode = 1;
        g_window_control_flag = 0;
    }
    if(window_data == 11)
    {
        g_window_work_mode = 1;
        g_window_control_flag = 1;
    }
    if(window_data == 12)
    {
        g_window_work_mode = 1;
        g_window_control_flag = 2;
    }
    if(window_data == 20)
    {
        g_window_work_mode = 2;
        g_window_control_flag = 0;
    }
    if(window_data == 21)
    {
        g_window_work_mode = 2;
        g_window_control_flag = 1;
    }
    if(window_data == 22)
    {
        g_window_work_mode = 2;
        g_window_control_flag = 2;
    }
    if(window_data == 30)
    {
        g_window_work_mode = 3;
        g_window_control_flag = 0;
    }
    if(window_data == 31)
    {
        g_window_work_mode = 3;
        g_window_control_flag = 1;
    }
    if(window_data == 32)
    {
        g_window_work_mode = 3;
        g_window_control_flag = 2;
    }
    if(led_data == 10)
    {
        g_led_work_mode = 1;
        g_led_control_flag = 0;
    }
    if(led_data == 11)
    {
        g_led_work_mode = 1;
        g_led_control_flag = 1;
    }
    if(led_data == 20)
    {
        g_led_work_mode = 2;
        g_led_control_flag = 0;
    }
    if(led_data == 21)
    {
        g_led_work_mode = 2;
        g_led_control_flag = 1;
    }
    if(led_data == 30)
    {
        g_led_work_mode = 3;
        g_led_control_flag = 0;
    }
    if(led_data == 31)
    {
        g_led_work_mode = 3;
        g_led_control_flag = 1;
    }
    if(garagedoor_data == 0)
    {
        g_garagedoor_control_flag = 0;
    }
    if(garagedoor_data == 1)
    {
        g_garagedoor_control_flag = 1;
    }
    if(garagedoor_data == 2)
    {
        g_garagedoor_control_flag = 2;
    }
    return;
}

void data_split(void)
{
    char door_str_data[2];
    char window_str_data[2];
    char led_str_data[2];
    char garagedoor_str_data[1];
    for(int i = 0; i < 2; i++)
    {
        door_str_data[i] = mq_buf[i];
    }
    door_data = atoi(door_str_data);
    window_str_data[0] = mq_buf[2];
    window_str_data[1] = mq_buf[3];
    window_data = atoi(window_str_data);
    led_str_data[0] = mq_buf[4];
    led_str_data[1] = mq_buf[5];
    led_data = atoi(led_str_data);
    garagedoor_str_data[0] = mq_buf[6];
    garagedoor_data = atoi(garagedoor_str_data);
}

/** 安防任务函数 **/
void Vars_task(void)
{
    while (1) 
    {
        data_split();
        data_translate();
        usleep(3 * 1000 * 1000);
    }
}

/** 安防任务创建函数 **/
void Vars_entry(void)
{
    usleep(3*1000*1000); //延时3S
    
    osThreadAttr_t attr;  //任务属性的结构体

    attr.name = "Vars_task";    //任务名称
    attr.stack_size = 4 * 1024;  //任务的内存大小
    attr.priority = 25;          //任务的优先级

    //创建任务
    if (osThreadNew((osThreadFunc_t)Vars_task, NULL, &attr) == NULL) 
    {
        //任务创建失败
        // PRINTF_DEBUG(2 , "  Failed to create Alarm_task!\r\n");
        printf("  Failed to create Alarm_task!\r\n");
    }
    //任务创建成功
    // PRINTF_DEBUG(0 , "  Succeed to create Alarm_task!\r\n");
    printf("  Succeed to create Alarm_task!\r\n");
}

/** 让系统运行指定的函数 **/
SYS_RUN(Vars_entry);